package com.future.mayi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.AddressModel;
import com.future.mayi.model.GoodsOrderModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsOrderEdtAddressActivity extends BaseTitleActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    private GoodsOrderModel model;

    public static void startActivity(Context context, GoodsOrderModel model) {
        Intent intent = new Intent(context, GoodsAllOrderActivity.class);
        intent.putExtra("model", model);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        getTitleText().setText("修改收货地址");
        initBack();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_goods_order_edt_address);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        model = (GoodsOrderModel) getIntent().getSerializableExtra("model");
        if (model == null) {
            finish();
            return;
        }

        setText(tvName, model.getAddress_user());
        setText(tvPhone, model.getAddress_tel());
        setText(tvAddress, model.getAddress());

    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.ll_address, R.id.tv_enter})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ll_address:
                Intent intent = new Intent(this, AddressActivity.class);
                intent.putExtra("isSelect", true);
                startActivityForResult(intent, 221);
                break;
            case R.id.tv_enter:
                if (addressModel==null){
                    toast("请选择新地址");
                    return;
                }
                toast("接口暂缺");
                showLoadingDialog();
                new HttpRequest<DefaultResponse>()
                        .with(this).setApiCode(ApiCst.NONE)
                        .addParam("token", App.getToken(mContext))
                        .addParam("address_id", addressModel.getAddress_id())
                        .addParam("order_id", model.getOrder_id())
                        .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                            @Override
                            public void onSuccess(DefaultResponse response) {
                                dissmissLoadingDialog();
                                toast(response.message);
                                finish();
                            }

                            @Override
                            public void onFail(String message, DefaultResponse response) {
                                dissmissLoadingDialog();
                                toast(message);
                            }
                        }).start(new DefaultResponse());

                break;
        }
    }
    AddressModel addressModel;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                if (requestCode == 221) {
                    addressModel = (AddressModel) data.getSerializableExtra("model");
                    tvName.setText(addressModel.getName());
                    tvPhone.setText(addressModel.getPhone());
                    tvAddress.setText(addressModel.getLocation() + addressModel.getDetail());
                }
            }
        }
    }

}
